/*
 * @Author: EyeSklear 1814580085@qq.com
 * @Date: 2024-01-08 10:27:01
 * @LastEditors: LIFM0623 1944099171@qq.com
 * @LastEditTime: 2024-03-06 15:05:28
 * @LastEditors: EyeSklear 1814580085@qq.com
 * @LastEditTime: 2024-02-22 15:53:55
 * @FilePath: \Scene_Back\src\user\user.controller.ts
 * @Description:
 *
 * Copyright (c) 2024 by EyeSklear , All Rights Reserved.
 */
/* eslint-disable prettier/prettier */
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Req,
  UploadedFile,
  UseInterceptors,
  Res,
} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { RegisterUserDto } from './dto/register-user.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { Request } from 'express';
import type { Response } from 'express';
import { Public } from 'src/common/decorator/public.decorator';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) { }

  @Post('register')
  @Public()
  register(@Body() registerUserDto: RegisterUserDto, @Req() request: Request) {
    // jk：取消 register 的邮箱验证
    // const authorizationHeader = (request.headers as any).authorization;
    // let registerToken: string | null = null;
    // if (authorizationHeader) {
    //   // 通常，Authorization 头部的格式是 "Bearer token"
    //   const [bearer, value] = authorizationHeader.split(' ');
    //   if (bearer === 'Bearer' && value) {
    //     registerToken = value;
    //   }
    // }
    // if (registerToken) {
    const registerToken = 'junke test token'
    return this.userService.register(registerUserDto, registerToken);
    // }
  }
  @Public()
  @Get('getUserInfo')
  getUserInfo(@Req() request: Request) {
    const authorizationHeader = (request.headers as any).authorization;
    let accessToken: string | null = null;
    if (authorizationHeader) {
      // 通常，Authorization 头部的格式是 "Bearer token"
      const [bearer, value] = authorizationHeader.split(' ');
      if (bearer === 'Bearer' && value) {
        accessToken = value;
      }
    }
    return this.userService.getUserInfo(accessToken);
  }

  @Post('updateUserInfo')
  @UseInterceptors(FileInterceptor('avatar'))
  updateUserInfo(@Body() updateUser: UpdateUserDto, @UploadedFile() avatar?) {
    // 此处有很大问题
    return this.userService.updateUserInfo(updateUser, avatar);
  }

  @Public()
  @Get('getUseravatar/:avatar')
  getUseravatar(@Param('avatar') avatar: string, @Res() res: Response) {
    return this.userService.getUseravatar(avatar, res)
  }

  @Post('addUserInfo')
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @Get('getAllUserInfo')
  findAll() {
    return this.userService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.userService.findOne(id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(id, updateUserDto);
  }

  @Delete('deleteUserById/:id')
  remove(@Param('id') id: string) {
    return this.userService.remove(id);
  }
}
